import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import { useUserStore } from "@/stores";

export const routes = [
  {
    path: "/",
    name: "index",
    components: {
      default: () => import("../views/login/index.vue"),
    },
  },
  {
    path: "/layout",
    name: "layout",
    meta: { title: "列表展示" },
    component: () =>
      import(/* webpackChunkName: "home" */ "../layout/index.vue"),
    children: [
      {
        // 用户钱包配置
        path: "/layout/userwallet",
        name: "userwallet",
        component: () =>
          import(
            /* webpackChunkName: "popup" */ "../views/userWallet/index.vue"
          ),
        meta: {
          f: true,
        },
      },
      {
        // 链信息配置
        path: "/layout/chainconfig",
        name: "chainconfig",
        component: () =>
          import(
            /* webpackChunkName: "popup" */ "../views/chainConfig/index.vue"
          ),
        meta: {
          f: true,
        },
      },
      {
        // 上币记录
        path: "/layout/record",
        name: "record",
        component: () =>
          import(/* webpackChunkName: "popup" */ "../views/record/index.vue"),
        meta: {
          f: true,
        },
      },
    ],
  },
  {
    // 登录页
    path: "/login",
    name: "login",
    components: {
      default: () => import("../views/login/index.vue"),
    },
  },
];
const router = createRouter({
  history: createWebHashHistory(),
  // history: createWebHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, _, next) => {
  const noToken = [
    "/login",
    "/register",
    "/product",
    "/developerCenter",
    "/",
    "/forget",
  ];
  if (noToken.includes(to.path)) {
    next();
  } else {
    const userStore = useUserStore();
    if (userStore.getToken) {
      next();
    } else {
      next("/login");
    }
  }
});

export default router;
